package org.benjamin.wechat.service.impl;

import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

public class WechatServiceImpl implements WechatService{
	
	private ObjectMapper objectMapper = new ObjectMapper();
	
	private final String token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	
	/**
	 * 图文消息内的图片URL上传
	 */
	private final String MEDIA_UPLOADIMG= "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN";
	
	private final String MEIDA_UPLOADNEWS = "http://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=ACCESS_TOKEN";
	
	public String getAccessToken(String appid,String appSecuret) throws ClientProtocolException, IOException {
		String tempToken = token.replace("APPID", appid).replace("APPSECRET", appSecuret);
		HttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(tempToken);
		HttpResponse httpResponse = httpClient.execute(httpGet);
		HttpEntity entity = httpResponse.getEntity();
		String json = EntityUtils.toString(entity);
		Map<String,String> resource = objectMapper.readValue(json, Map.class);
		return resource.get("access_token");
	}
	
	public void uploadNews() {
		
	}

}
